package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.data.HttpResponseEnum;
import com.dongdongshop.data.Result;
import com.dongdongshop.pojo.Cart;
import com.dongdongshop.pojo.comment.Comment;
import com.dongdongshop.pojo.order.TbOrder;
import com.dongdongshop.pojo.order.TbOrderItem;
import com.dongdongshop.pojo.user.TbUser;
import com.dongdongshop.service.*;
import com.dongdongshop.util.IdWorker;
import com.dongdongshop.util.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@CrossOrigin//支持跨域的注解
@Controller
@RequestMapping("comment")
public class CommentController {

    private final Logger logger= LoggerFactory.getLogger(CommentController.class);
    @Reference
    private OrderServiceI orderService;
    @Reference
    private SellerServiceI sellerService;
    @Reference
    private OrderItemServiceI orderItemService;

    @Reference
    private ItemServiceI itemService;
    @Reference
    private CommentServiceI commentServiceI;

    //跳转评价页面
    @RequestMapping("toComment")
    public String toComment(){
        return "home-order-evaluate";
    }

    //获取评价商品的信息
    @RequestMapping("getCommentByGoods")
    @ResponseBody
    public Result getCommentByGoods(){
        try {
            TbUser user =(TbUser) SecurityUtils.getSubject().getPrincipal();
            if (user == null){
                return Result.jsonResponse(HttpResponseEnum.UNATUTH);
            }
            List<Cart> cartList=new ArrayList<>();
            String username=user.getUsername();
            List<TbOrder> orderList=orderService.selectComment(username);
            for (TbOrder order : orderList) {
                Cart cart=new Cart();
                cart.setSellerId(order.getSellerId());
                String sellerName = sellerService.getNameById(order.getSellerId());
                cart.setSellerName(sellerName);
                List<TbOrderItem> orderItemList = orderItemService.selectByOrderId(order.getOrderId());
                for (TbOrderItem orderItem : orderItemList) {
                    orderItem.setIdStr(String.valueOf(orderItem.getId()));
                }
                cart.setOrderItemList(orderItemList);
                cart.setPayOrder(String.valueOf(order.getPayOrder()));
                cartList.add(cart);
              //  System.out.println(cartList);
            }
            return  Result.jsonResponse(HttpResponseEnum.OK).setData(cartList);
        }catch (Exception e){
            logger.info("数据错误");
            return Result.jsonResponse(HttpResponseEnum.FAIL);
        }
    }
    //跳转增加评价页面
    @RequestMapping("toEvaluate")
    public String toEvaluate(String id, Model model){
        model.addAttribute("id",id);
        return "evaluate";
    }
    //增加评论
    @RequestMapping("addComment")
    @ResponseBody
    public Result addComment(String content,int score,String level,String pic,String id){
        System.out.println("信息内容"+content+":"+score+":"+level);
        try {
            TbUser user =(TbUser) SecurityUtils.getSubject().getPrincipal();
            //根据id找到itemId
            Long itemId= orderItemService.selectItemById(Long.parseLong(id));
            System.out.println("--------"+itemId);
            //根据itemId找到goodsId
            Long goodsId=itemService.selectGoodsById(itemId);
            Comment comment=new Comment();
            comment.setId(new IdWorker().nextId()+"");
            comment.setGoodId(goodsId);
            comment.setUserId(user.getId());
            comment.setUserName(user.getUsername());
            comment.setStartTime(new Date());
            comment.setAddTime(new Date());
            comment.setContent(content);
            comment.setLevel(level);
            comment.setStarts(score);
            comment.setPic(pic);
            long l = Long.parseLong("0");
            comment.setLikeNum(l);
            comment.setParentId("0");
            commentServiceI.addComment(comment);
            System.out.println("增加成功："+comment);
            return Result.jsonResponse(HttpResponseEnum.OK);
        }catch (Exception e){
            e.printStackTrace();
            logger.info("数据错误");
            return Result.jsonResponse(HttpResponseEnum.FAIL);
        }
    }


    @RequestMapping("getCommentByGoodsId")
    @ResponseBody
    public Result getCommentByGoodsId(Long goodsId, @RequestParam(defaultValue = "1")Integer pageNumber, @RequestParam(defaultValue = "3")Integer pageSize){
        try {
            //System.out.println(goodsId);
            PageInfo<Comment> pageInfo = commentServiceI.getCommentByGoodsId(goodsId,pageNumber,pageSize);
            System.out.println(pageInfo.getList());
            return Result.jsonResponse(HttpResponseEnum.OK).setData(pageInfo);
        }catch (Exception e){
            logger.error("操作异常",e);
            return Result.jsonResponse(HttpResponseEnum.FAIL);
        }

    }

    //点赞
    @RequestMapping("updateLikeNum")
    @ResponseBody
    public Result updateLikeNum(String id){
        try {
            commentServiceI.updateLikeNum(id);
            return Result.jsonResponse(HttpResponseEnum.OK);
        }catch (Exception e){
            logger.error("操作异常",e);
            return Result.jsonResponse(HttpResponseEnum.FAIL);
        }
    }

    //回复评论
    @RequestMapping("toEvaluateReply")
    public String toEvaluateReply(String id, Long goodsId, Model model){
        model.addAttribute("id", id);
        model.addAttribute("goodsId", goodsId);
        return "home-order-evaluatereply";
    }
    @RequestMapping("insertEvluateReply")
    @ResponseBody
    public Result insertEvluateReply(String id,Long goodsId, String reply){
        commentServiceI.insertEvluateReply(id, reply);
        return Result.jsonResponse(HttpResponseEnum.OK);
    }

}
